package com.rmv.ums.response;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;

public abstract class AbstractResponse implements Response {

	// configuration
	protected String id;
	protected String value;
	protected String clazz;
	
	public boolean reference=false;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getClassName() {
		return clazz;
	}

	public void setClassName(String className) {
		this.clazz = className;
	}

	public boolean isReference() {
		return reference;
	}

	public void setReference(boolean isReference) {
		this.reference=isReference;
	}

	// response method
	public abstract void process(HttpServletRequest request, HttpServletResponse response) throws IOException;

	public boolean configure(Element element) {
		// id
		this.id = element.attributeValue("id");
		if(StringUtils.isBlank(this.id)) {
			this.id = "RESP"+RandomStringUtils.randomAlphanumeric(8);
		} else {
			this.reference = true;
		}


		// class
		this.clazz = element.attributeValue("class");

		// value
		this.value = element.getTextTrim();

		return true;
	}
	
	public String toString() {
		return "[response:id=" + id + ",value=" + value + "]";
	}
}
